package com.wangyu.springsecuritydemo.config;

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 数据源配置
 *
 * @Author wangyu
 * @Date 2019/2/21 17:23
 * @Version 1.0
 */
@Configuration
public class DataSourceConfigurer {

  @Bean("primaryDataSource")
  @Primary
  @ConfigurationProperties("spring.dataSource.primary")
  public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
  }

  @Bean("secondaryDataSource")
  @ConfigurationProperties("spring.datasource.secondary")
  public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
  }

  /**
   * secondaryJdbcTemplate JdbcTemplate
   */
  @Bean(name = "secondaryJdbcTemplate")
  public JdbcTemplate secondaryJdbcTemplate(
      @Qualifier("secondaryDataSource") DataSource dataSource) {
    return new JdbcTemplate(dataSource);
  }
}
